import qs from "qs";
import axios from "axios";
import { IRank, IAction, IData } from "./interface";
const rank: IRank = {
  namespaced: true,
  state: {
    data: [],
    loading: false,
    nav: [
      {
        id: 0,
        data: "实时榜",
        cid: "0",
      },
      {
        id: 1,
        data: "全天榜",
        cid: "-1",
      },
      {
        id: 2,
        data: "居家日用",
        cid: "6",
      },
      {
        id: 3,
        data: "美食",
        cid: "4",
      },
      {
        id: 4,
        data: "女装",
        cid: "1",
      },
      {
        id: 5,
        data: "美妆",
        cid: "3",
      },
      {
        id: 6,
        data: "数码家电",
        cid: "8",
      },
      {
        id: 7,
        data: "鞋品",
        cid: "5",
      },
      {
        id: 8,
        data: "男装",
        cid: "9",
      },
      {
        id: 9,
        data: "内衣",
        cid: "10",
      },
      {
        id: 10,
        data: "母婴",
        cid: "2",
      },
      {
        id: 11,
        data: "家装家纺",
        cid: "14",
      },
      {
        id: 12,
        data: "文娱车品",
        cid: "7",
      },
      {
        id: 13,
        data: "配饰",
        cid: "12",
      },
      {
        id: 14,
        data: "箱包",
        cid: "11",
      },
      {
        id: 15,
        data: "户外运动",
        cid: "13",
      },
    ],
  },
  actions: {
    async getssb({ commit }) {
      const res: IData[] = await axios(
        "http://cmsjapi.ffquan.cn/api/goods/rushing-ranking/goods-list2/v2?type=1&cId=0"
      ).then((res): IData[] => {
        return res.data.data;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    async getqtb({ commit }) {
      const res: IData[] = await axios(
        "http://cmsjapi.ffquan.cn/api/goods/rushing-ranking/goods-list2/v2?type=2&cId=0"
      ).then((res): IData[] => {
        return res.data.data;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    async getdata({ commit }, { cid }) {
      const res: IData[] = await axios(
        `http://cmsjapi.ffquan.cn/api/goods/rushing-ranking/goods-list2/v2?type=1&cId=${cid}`
      ).then((res): IData[] => {
        return res.data.data;
      });
      const action: IAction = {
        type: "SETDATA",
        payload: res,
      };
      commit(action);
    },
    setload({ commit }) {
      const action: { type: string } = {
        type: "SETLOAD",
      };
      commit(action);
    },
  },
  mutations: {
    SETDATA(state, action) {
      state.data = action.payload;
      state.loading = false;
    },
    SETLOAD(state) {
      state.loading = true;
    },
  },
};
export default rank;
